/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package PictureDescription;

import java.util.HashMap;
import java.util.Vector;

/**
 * @author Christoph Schnack
 *
 */
public class Lesson {
	HashMap<String,Vector<String>> units;
	PictureStorage PICSTO;
	
	public Lesson(PictureStorage picsto){
		units = new HashMap<String,Vector<String>>();
		PICSTO = picsto;
	}
	
	public boolean hasUnits(){
		return !units.isEmpty();
	}
	
	public Vector<PictureUnit> getPictureUnits(){
		if(units.size()>0){
			Vector<PictureUnit> pus = new Vector<PictureUnit>();
			for(String col : getCollectionNames()){
				PictureCollection pc = PICSTO.getCollection(col);
				for(String nme : getUnitNames(col)){
					pus.add(pc.getPictureUnit(nme));
				}
			}
			return pus;
		}
		else return null;
	}
	
	public String[] getCollectionNames(){
		return units.keySet().toArray(new String[]{}); 
	}
	
	public String[] getUnitNames(String coll){
		return units.get(coll)==null?null:units.get(coll).toArray(new String[]{});
	}
	
	public void changeCollectionName(String oldName,String newName){
		if(!units.isEmpty() && units.containsKey(oldName)){
			units.put(newName, units.get(oldName));
			units.remove(oldName);
		}
	}
	
	public void deleteCollection(String toDelete){
		if(!units.isEmpty() && units.containsKey(toDelete)){
			units.remove(toDelete);
		}
	}
	
	
	/**
	 * Deletes either all occurences of the unit or only one.
	 * 
	 * @param coll
	 * @param unit
	 * @param deleteAllUnits
	 */
	public void deleteUnitFromCollection(String coll, String unit,boolean deleteAllUnits){
		if(units.containsKey(coll)){
			Vector<String> collection = units.get(coll);
			System.out.println("collection: "+coll + "   size: "+collection.size());
			Vector<Integer> indexes = new Vector<Integer>();
			String name;
			for(int i=0;i<collection.size();i++){
				name = collection.elementAt(i);
				if(name.equals(unit)){
					if(!deleteAllUnits){
						collection.removeElementAt(i);
						break;
					}
					else{
						indexes.add(i);
					}
				}
			}
			if(deleteAllUnits && indexes.size()>0){
				for(int i=indexes.size()-1;i>=0;i--){
					collection.removeElementAt(indexes.elementAt(i));
				}
			}
			if(collection.isEmpty()) units.remove(coll);
		}
	}
	
	/**
	 * Adds the name of a pictureUnit to the lesson.
	 * 
	 * @param collection to add to
	 * @param name of the picture unit
	 */
	public void addUnit(String collection, String name){
		if(collection!=null && name!=null && !collection.equals("") && !name.equals("")){
			if(units.containsKey(collection))units.get(collection).add(name);
			else{
				units.put(collection, new Vector<String>());
				units.get(collection).add(name);
			}
		}
	}
	
	
	/**
	 * Alters the name of a pictureUnit in a Lesson.
	 * 
	 * @param collection of the pictureUnit
	 * @param oldName of the pictureUnit
	 * @param newName of the pictureUnit
	 */
	public void alterUnitName(String collection,String oldName, String newCollection, String newName){
		if(!units.isEmpty() && units.containsKey(collection)){
			Vector<String> names = units.get(collection);
			int size = names.size();
			Vector<Integer> indexes = new Vector<Integer>();
			if(size>0 && names!=null){
				for(int i=0;i<size;i++){
					if(names.elementAt(i).equals(oldName))indexes.add(i);
				}
				for(int i=indexes.size()-1;i>=0;i--){
					names.removeElementAt(indexes.elementAt(i));
				}
				for(int i=0;i<indexes.size();i++){
					addUnit(newCollection,newName);
				}
			}
		}
	}
}
